package com.javafly.sms.web.security;

import java.util.List;

import javax.servlet.ServletContext;

import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import com.javafly.sms.entity.security.Authority;
import com.javafly.sms.entity.security.Resource;
import com.javafly.sms.entity.security.Role;
import com.javafly.sms.service.security.SecurityEntityManager;
import com.javafly.sms.web.CrudActionSupport;

import org.springside.modules.orm.Page;
import org.springside.modules.orm.PropertyFilter;
import org.springside.modules.orm.hibernate.HibernateWebUtils;
import org.springside.modules.web.struts2.Struts2Utils;

/**
 * 权限管理Action.
 * 
 * 演示不分页的简单管理界面.
 * 
 * @author calvin
 */
@Namespace("/security")
@Results( { @Result(name = CrudActionSupport.RELOAD, location = "auth.action", type = "redirect") })
public class AuthAction extends CrudActionSupport<Authority> {

	private static final long serialVersionUID = 1L;

	@Autowired
	private SecurityEntityManager securityEntityManager;

	//-- 页面属性 --//
	private Long id;
	private Long idt;
	private Page<Authority> page = new Page<Authority>(20);//每页20条记录
	public Page<Authority> getPage() {
		return page;
	}
	public Long getIdt() {
		idt=120L;
		return idt;
	}
	public Long getId() {
		id=122L;
		return id;
	}
	private Authority entity;
	private List<Authority> allAuthList;//权限列表
	private List<Long> checkedResourceIds;//页面中钩选的资源id列表
	private String str;
	public String getStr() {
		str="haha 哈哈";
		return str;
	}
     
	public void setStr(String str) {
		this.str = str;
	}

	//-- ModelDriven 与 Preparable函数 --//
	public Authority getModel() {
		return entity;
	}

	public void setId(Long id) {
		this.id = id;
	}

	@Override
	protected void prepareModel() throws Exception {
		if (id != null) {
			entity = securityEntityManager.getAuth(id);
		} else {
			entity = new Authority();
		}
	}

	//-- CRUD Action 函数 --//
	@Override
	public String list() throws Exception {
		
		List<PropertyFilter> filters = HibernateWebUtils.buildPropertyFilters(Struts2Utils.getRequest());
		//设置默认排序方式
		if (!page.isOrderBySetted()) {
			page.setOrderBy("id");
			page.setOrder(Page.ASC);
		}
		page = securityEntityManager.searchAuth(page, filters);
//		allAuthList = securityEntityManager.getAllAuthority();
//		securityEntityManager.resetRight();
		return SUCCESS;
	}

	@Override
	public String input() throws Exception {
		checkedResourceIds = entity.getResourceIds();
		return INPUT;
	}

	@Override
	public String save() throws Exception {
		//根据页面上的checkbox 整合Role的Authorities Set. 
		HibernateWebUtils.mergeByCheckedIds(entity.getResourceList(), checkedResourceIds, com.javafly.sms.entity.security.Resource.class);
		//保存用户并放入成功信息.
		securityEntityManager.saveAuth(entity);
//		org.springframework.security.context.
		//org.springside.modules.utils.SpringContextUtils.getApplicationContext();
		//ServletContext ctx11=Struts2Utils.getRequest().getSession().getServletContext();
		securityEntityManager.resetRight();
		
		addActionMessage("保存权限成功");
		
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		
		entity = securityEntityManager.getAuth(id);
		if(entity.getResourceList()==null||entity.getResourceList().size()==0){
			securityEntityManager.deleteAuth(id);
			addActionMessage("删除权限成功");
		}else{
			addActionMessage("删除权限失败，请先去除关联的资源！");
		}
		return RELOAD;
	}

	//-- 页面属性访问函数 --//
	/**
	 * list页面显示所有角色列表.
	 */
	public List<Authority> getAllAuthList() {
		return allAuthList;
	}

	/**
	 * input页面显示所有授权列表.
	 */
	public List<Authority> getAllAuthorityList() {
		return securityEntityManager.getAllAuthority();
	}
	/**
	 * input页面显示菜单资源列表.
	 */
	public List<com.javafly.sms.entity.security.Resource> getMenuResourceList() {
		return securityEntityManager.getMenuResource();
	}
	public String getDoStr(){
		String dostr="";
		List<com.javafly.sms.entity.security.Resource> slist = securityEntityManager.getDoResource();
    	for(int i=0;i<slist.size();i++){
    		com.javafly.sms.entity.security.Resource res = slist.get(i);
    		dostr=dostr+"<input type=\"checkbox\" name=\"checkedResourceIds\" value=\""+res.getId()+"\"  "+getcheckstr(res.getId(),entity.getResourceIds())+"/><label >"+res.getShowname()+"</label>";   		
    	}
		return dostr;
	}
    public String getMenuStr(){
    	String menustr="<ul class=\"tree\" style=\"margin-left: 15px;\">";
    	List<com.javafly.sms.entity.security.Resource> slist = securityEntityManager.getMenuResource();
    	for(int i=0;i<slist.size();i++){
    		com.javafly.sms.entity.security.Resource res = slist.get(i);
    		if(res.getUpmenu()==-1){
    			menustr=menustr+"<li>	    " +
    					"<input type=\"checkbox\" name=\"checkedResourceIds\" value=\""+res.getId()+"\"  "+getcheckstr(res.getId(),entity.getResourceIds())+">" +
    					"<label>"+res.getShowname()+"</label>";
    			menustr=menustr+getsubstr(res.getId(),slist);
    			menustr=menustr+"</li>";
    		}
    	}
    	menustr=menustr+"</ul>";
    	return menustr;
    }
    public String getcheckstr(Long pid, List<Long> slist){
    	for(int i=0;i<slist.size();i++){
    		Long tmpl=slist.get(i);
    		if(tmpl.equals(pid)){
    			return "checked=\"checked\"";
    		}
    	}
    	return "";
    }
    
    
    
    
    public String getsubstr(Long pid,List<com.javafly.sms.entity.security.Resource> slist){
    	String substr="";
    	int k=0;
    	for(int i=0;i<slist.size();i++){
    		com.javafly.sms.entity.security.Resource res = slist.get(i);
    		if(res.getUpmenu().equals(pid)){ 
    			k++;
    			if(k==1){
    				substr=substr+"<ul>";
    			}
    			substr=substr+"<li>" +
    					"<input type=\"checkbox\" name=\"checkedResourceIds\" value=\""+res.getId()+"\"    "+getcheckstr(res.getId(),entity.getResourceIds())+">" +
    					"<label>"+res.getShowname()+"</label>";
				substr=substr+getsubstr(res.getId(),slist);
    			substr=substr+"</li>";
    		}
    	}
    	if(k!=0){
    		substr=substr+"</ul>";
    	}
    	return substr;
    }
	/**
	 * input页面显示权限拥有的资源.
	 */
	public List<Long> getCheckedResourceIds() {
		return checkedResourceIds;
	}

	/**
	 * input页面提交权限拥有的资源.
	 */
	public void setCheckedResourceIds(List<Long> checkedResourceIds) {
		this.checkedResourceIds = checkedResourceIds;
	}
}